package com.chuangyang.fixboxlib.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.Button;

import com.chuangyang.fixboxlib.R;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by vasnhu on 2015/3/21.
 */
public class VerificationCodeButton extends Button {
    private static final int DEFAULT_TIMES=60;
    private int  times = DEFAULT_TIMES;
    private final TimerHandler timerHandler = new TimerHandler(this);
    private static String defaultText;
    private Timer timer;
    private TimerTask timerTask;
    private boolean isLoading=false;
    private int disableBackground;
    private Drawable defaultBackground;

    public VerificationCodeButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        defaultText=getText().toString();
        final TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.VerificationCodeButton, 0, 0);
        disableBackground =a.getColor(R.styleable.VerificationCodeButton_disable_background, 0);
        defaultBackground=a.getDrawable(R.styleable.VerificationCodeButton_default_background);
        setBackgroundDrawable(defaultBackground);
        a.recycle();

    }


    private  class MyTask extends TimerTask{
        @Override
        public void run() {
            Message message = new Message();
            message.arg1 = times;
            message.what = 0;
            timerHandler.sendMessage(message);
            times--;
            if (times == 0) {
                timerTask.cancel();
                timer.cancel();
                timer=null;
                timerHandler.sendEmptyMessage(1);
            }
        }
    }

    public boolean isLoading(){
        return isLoading;
    }


    public void start(){
        isLoading=true;
        timer = new Timer();
        timerTask =new MyTask();
        timer.schedule(timerTask, 0, 1000);
        setClickable(false);
        setFocusable(false);
        setBackgroundColor(disableBackground);
    }
    
    private void reset(VerificationCodeButton timerWidgets){
        isLoading=false;
        timerWidgets.setClickable(true);
        timerWidgets.setFocusable(true);
        timerWidgets.setText(defaultText);
        times=DEFAULT_TIMES;
        setBackgroundDrawable(defaultBackground);
    }

    static class TimerHandler extends Handler {
        private final WeakReference<VerificationCodeButton> mWeakreference;

        TimerHandler(VerificationCodeButton service) {
            mWeakreference = new WeakReference<VerificationCodeButton>(service);
        }

        @Override
        public void handleMessage(Message msg) {
            VerificationCodeButton timerWidgets = mWeakreference.get();
            switch (msg.what) {
                case 0:
                    if (timerWidgets != null) {
                        timerWidgets.setText(msg.arg1 + "秒后可重发");
                    }
                    break;
                case 1:
                    if (timerWidgets != null) {
                        timerWidgets.reset(timerWidgets);
                    }
                    break;
            }
        }
    }

}
